﻿using Furion;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.Logging.Extensions;
using Htx.Fur.Application.System.Dtos;
using Htx.Fur.Core.Entities;
using Htx.Fur.Core.Options;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Htx.Fur.Application
{
    /// <summary>
    /// 系统服务接口
    /// </summary>
    public class SystemAppService : IDynamicApiController
    {
        private readonly ISystemService _systemService;
        public SystemAppService(ISystemService systemService)
        {
            _systemService = systemService;
        }

        /// <summary>
        /// 获取系统描述
        /// </summary>
        /// <returns></returns>
        public string GetDescription()
        {
            return _systemService.GetDescription();
        }

        /// <summary>
        /// 获取应用信息11
        /// </summary>
        /// <returns></returns>
        public AppInfoOptions GetAppInfo()
        {
            // 读取选项
            var appInfo = App.GetConfig<AppInfoOptions>("AppInfo", true);
            return appInfo;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public UserDto GetUser()
        {
            return _systemService.GetUser();
        }

        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public UserDto CreateUser(UserDto user)
        {
            var entity = _systemService.CreateUser(user.Adapt<UserInfo>());
            return entity;
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserDto>> GetUserListAsync()
        {
            return await _systemService.GetUserListAsync();
        }

        /// <summary>
        /// 删除用户（通过事件总线）
        /// </summary>
        public void DeleteUser()
        {
            MessageCenter.Send("delete:user", new { Name = "孙小双", Age = 22 });
        }

        public async Task<int> GetQueueNumber([FromServices] IDistributedCache cache)
        {
            var buffer = await cache.GetStringAsync("QueueNumber") ?? "0";
            var number = int.Parse(buffer);
            number++;
            await cache.SetStringAsync("QueueNumber", number.ToString());
            return number;
        }

        public string Log(string msg)
        {
            msg.LogDebug();
            msg.LogInformation();
            msg.LogWarning();
            msg.LogError();
            msg.LogCritical();
            return msg;
        }

        public string Log2(string msg)
        {
            msg.LogDebug();
            throw new Exception();
            return msg;
        }

    }
}
